package com.listener;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * @Auther: Min.Liu
 * @Description:
 * @Date: Created in 9:42 2025/4/25
 */
public class mySpringApplicationRunListener implements SpringApplicationRunListener , Ordered {
	private final SpringApplication application;

	private final String[] args;

	private final SimpleApplicationEventMulticaster initialMulticaster;

	public mySpringApplicationRunListener(SpringApplication application, String[] args) {
		this.application = application;
		this.args = args;
		this.initialMulticaster = new SimpleApplicationEventMulticaster();
		for (ApplicationListener<?> listener : application.getListeners()) {
			this.initialMulticaster.addApplicationListener(listener);
		}
	}

	@Override
	public int getOrder() {
		return Integer.MAX_VALUE-1;
	}


	@Override
	public void starting() {
		System.out.println("早期的执行，最早");
	}

	@Override
	public void environmentPrepared(ConfigurableEnvironment environment) {
		System.out.println("ApplicationContext还没创建,ConfigurableEnvironment已经准备好-->"+environment);
	}

	@Override
	public void contextPrepared(ConfigurableApplicationContext context) {
		System.out.println("ApplicationContext--->"+context+"已经创建和准备好,但是对应资源还没加载");
	}

	@Override
	public void contextLoaded(ConfigurableApplicationContext context) {
		System.out.println("ApplicationContext--->"+context+"对应资源加载完毕,但是容器还未刷新");
	}

	@Override
	public void started(ConfigurableApplicationContext context) {
		System.out.println("ApplicationContext--->"+context+"执行了刷新并且启动了,但是CommandLineRunners和ApplicationRunner的回调还没执行");
	}

	@Override
	public void running(ConfigurableApplicationContext context) {
		System.out.println("run方法即将执行完毕,ApplicationContext--->"+context+"执行了刷新并且启动了,所有的CommandLineRunners和ApplicationRunner的回调执行完毕");
	}

	@Override
	public void failed(ConfigurableApplicationContext context, Throwable exception) {

	}
}
